﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGGCore;

namespace IGG
{
    public class ModeOverviewFormatter : IModeFormatter
    {
        #region IModeFormatter Members

        private const int WIDTH = 39;

        /// <summary>
        /// Format a star description
        /// </summary>
        public static void FormatStar(int index, Player star)
        {
            string starStr = "";
            Output.WriteLine(ToolBox.GetFormattedStringPadRight(" " + index + ": " 
                + star.TeamAbbr + " " + star.LastName, 25)
                + star.GameLines[0]);

            if (star.AVG == null)
            {
                // PITCHER
                float era = float.Parse(star.ERA);
                starStr += star.NameDisplay + " has an " + star.ERA + " ERA.";
                Output.FormatLongText(starStr);
            }
            else
            {
                string avstr = star.AVG.Replace(".", "");
                int avg = int.Parse(avstr);
                if (avg > 299)
                {
                    Output.Write(" " + star.NameDisplay + " is hitting for " + star.AVG);
                    if (ToolBox.IsPreseason())
                    {
                        Output.Write(" this spring.");
                    }
                    else
                    {
                        Output.Write(" this season.");
                    }
                    if (star.SeasonHomeruns > 1)
                    {
                        Output.Write(" He also has " + star.SeasonHomeruns + " hr");
                        if (star.SeasonRBI > 1)
                        {
                            Output.Write(" and " + star.SeasonRBI + " rbi");
                        }
                        Output.Write(".");
                    }
                    else if (star.SeasonRBI > 1)
                    {
                        Output.Write(" He also has " + star.SeasonRBI + " rbi.");
                    }
                    Output.WriteLine();
                }
                else if (star.SeasonHomeruns > 1)
                {
                    Output.Write(" " + star.NameDisplay + " has hit "
                        + star.SeasonHomeruns + " hr");
                    if (ToolBox.IsPreseason())
                    {
                        Output.Write(" this spring.");
                    }
                    else
                    {
                        Output.Write(" this season.");
                    }
                    if (star.SeasonRBI > 1)
                    {
                        Output.Write(" He has " + star.SeasonRBI + " rbi.");
                    }
                    Output.WriteLine();
                }
                else if (avg > 100)
                {
                    string adjective = "a good";
                    if (avg < 100)
                    {
                        adjective = "an atrocious";
                    }
                    else if (avg < 200)
                    {
                        adjective = "an awful";
                    }
                    else if (avg < 230)
                    {
                        adjective = "a very quiet";
                    }
                    else if (avg < 250)
                    {
                        adjective = "a quiet";
                    }
                    else if (avg < 280)
                    {
                        adjective = "an ok";
                    }
                    Output.Write(" " + star.NameDisplay + " has " + adjective);
                    if (ToolBox.IsPreseason())
                    {
                        Output.Write(" spring");
                    }
                    else
                    {
                        Output.Write(" season");
                    }
                    Output.Write(" (" + star.AVG + ").");
                    Output.WriteLine();
                }
                else
                {

                }
                
            }
            Output.WriteLine();
        }

        private static int SubstractInnings(int first, int second)
        {
            if (second == -1)
            {
                return first;
            }
            else
            {
                return first - second;
            }
        }

        private static int SubstractInnings(int first, int second, int third)
        {
            return SubstractInnings(SubstractInnings(first, second), third);
        }

        private static int SubstractInnings(int first, int second, int third, int fourth)
        {
            return SubstractInnings(SubstractInnings(SubstractInnings(first, second), third), fourth);
        }

        /// <summary>
        /// Format a terminated game
        /// </summary>
        public static void FormatTerminatedGame(Game game, bool mentionYesterday)
        {
            string gamestr = "";
            //if (!mentionYesterday)
            //{
            //    gamestr = "Today, ";
            //}
            if (game.AwayScore == game.HomeScore)
            {
                gamestr += "There was a tie between the " + game.AwayTeam.Name + " and the " + game.HomeTeam.Name + " at " + game.Stadium.Name + ". Only in spring... ";
            }
            else
            {
                Team winningTeam = game.AwayTeam;
                Team losingTeam = game.HomeTeam;
                if (game.AwayScore < game.HomeScore)
                {
                    losingTeam = game.AwayTeam;
                    winningTeam = game.HomeTeam;
                }
                gamestr       += "The " + winningTeam.Name + " defeated the " + losingTeam.Name + " at " + game.Stadium.Name + ". ";
                if (game.Linescore.Innings.Count > 7)
                {
                    int scoreAway8 = SubstractInnings(game.AwayScore, game.Linescore.Innings[8].Away);
                    int scoreHome8 = SubstractInnings(game.HomeScore, game.Linescore.Innings[8].Home);
                    int scoreAway7 = SubstractInnings(game.AwayScore, game.Linescore.Innings[8].Away, game.Linescore.Innings[7].Away);
                    int scoreHome7 = SubstractInnings(game.HomeScore, game.Linescore.Innings[8].Home, game.Linescore.Innings[7].Home);
                    int scoreAway6 = SubstractInnings(game.AwayScore, game.Linescore.Innings[8].Away, game.Linescore.Innings[7].Away, game.Linescore.Innings[6].Away);
                    int scoreHome6 = SubstractInnings(game.HomeScore, game.Linescore.Innings[8].Home, game.Linescore.Innings[7].Home, game.Linescore.Innings[6].Home);
                    if (game.Linescore.Innings.Count > 9)
                    {
                        gamestr += "That one went into extra innings! ";
                    }
                    else if (scoreAway8 == scoreHome8)
                    {
                        gamestr += "The " + winningTeam.Name + " broke the tie in the ninth. ";
                    }
                    else if (scoreAway7 == scoreHome7)
                    {
                        gamestr += "The score was tied in the seventh " + scoreAway7 + " to " + scoreHome7 + ". ";
                        if (scoreAway8 > scoreHome8)
                        {
                            gamestr += game.AwayTeam.City + " took the lead in the 8th";
                            if ((scoreAway8 - scoreHome8) > 1)
                            {
                                gamestr += " with a burst of " + game.Linescore.Innings[7].Away + " points. ";
                            }
                            else
                            {
                                gamestr += ". ";
                            }
                        }
                        else if (scoreAway8 < scoreHome8)
                        {
                            gamestr += game.HomeTeam.City + " took the lead in the 8th";
                            if ((scoreHome8 - scoreAway8) > 1)
                            {
                                gamestr += " with a burst of " + game.Linescore.Innings[7].Home + " points. ";
                            }
                            else
                            {
                                gamestr += ". ";
                            }
                        }
                    }
                    else if (scoreAway8 < scoreHome8 && game.AwayScore > game.HomeScore)
                    {
                        gamestr += "The " + winningTeam.Name + " were trailing by " + (scoreHome8 - scoreAway8) + " in the eight. ";
                    }
                    else if (scoreAway8 > scoreHome8 && game.AwayScore < game.HomeScore)
                    {
                        gamestr += "The " + winningTeam.Name + " were trailing by " + (scoreAway8 - scoreHome8) + " in the eight. ";
                    }
                    else if (scoreAway7 < scoreHome7 && game.AwayScore > game.HomeScore)
                    {
                        if (scoreAway6 < scoreHome6 && ((scoreHome6 - scoreAway6) > (scoreHome7 - scoreAway7)))
                        {
                            gamestr += "The " + winningTeam.Name + " were trailing by " + (scoreHome6 - scoreAway6) + " in the sixth. ";
                        }
                        else
                        {
                            gamestr += "The " + winningTeam.Name + " were trailing by " + (scoreHome7 - scoreAway7) + " in the seventh. ";
                        }

                    }
                    else if (scoreAway7 > scoreHome7 && game.AwayScore < game.HomeScore)
                    {
                        if (scoreAway6 > scoreHome6 && ((scoreAway6 - scoreHome6) > (scoreAway7 - scoreHome7)))
                        {
                            gamestr += "The " + winningTeam.Name + " were trailing by " + (scoreAway6 - scoreHome6) + " in the sixth. ";
                        }
                        else
                        {
                            gamestr += "The " + winningTeam.Name + " were trailing by " + (scoreAway7 - scoreHome7) + " in the seventh. ";
                        }
                    }
                    else
                    {
                        if (game.AwayScore > game.HomeScore)
                        {
                            int idex = 1;
                            foreach (Inning inn in game.Linescore.Innings)
                            {
                                if (inn.Away == game.AwayScore)
                                {
                                    gamestr += "The " + winningTeam.Name + " made all their points in the " + idex + ". ";
                                    break;
                                }
                                idex++;
                            }
                        }
                        else if (game.AwayScore < game.HomeScore)
                        {
                            int idex = 1;
                            foreach (Inning inn in game.Linescore.Innings)
                            {
                                if (inn.Home == game.HomeScore)
                                {
                                    gamestr += "The " + winningTeam.Name + " made all their points in the " + idex + ". ";
                                    break;
                                }
                                idex++;
                            }
                        }
                        if (Math.Abs(game.AwayScore - game.HomeScore) == 1)
                        {
                            gamestr += "This was a real close game. ";
                        }
                    }
                }
                else
                {
                    if (game.AwayScore > game.HomeScore)
                    {
                        int idex = 1;
                        foreach (Inning inn in game.Linescore.Innings)
                        {
                            if (inn.Away == game.AwayScore)
                            {
                                gamestr += "The " + winningTeam.Name + " made all their points in the " + idex + ". ";
                                break;
                            }
                            idex++;
                        }
                    }
                    else if (game.AwayScore < game.HomeScore)
                    {
                        int idex = 1;
                        foreach (Inning inn in game.Linescore.Innings)
                        {
                            if (inn.Home == game.HomeScore)
                            {
                                gamestr += "The " + winningTeam.Name + " made all their points in the " + idex + ". ";
                                break;
                            }
                            idex++;
                        }
                    }
                    if (Math.Abs(game.AwayScore - game.HomeScore) == 1)
                    {
                        gamestr += "This was a real close game. ";
                    }
                }
                string firstLead = null;
                bool leadAway = false;
                bool tied = true;
                bool leadKept = true;
                int scoreHome = 0;
                int scoreAway = 0;
                int inningFirstLead = 0;
                int inningNum = 0;
                foreach (Inning inning in game.Linescore.Innings)
                {
                    inningNum++;
                    scoreAway += inning.Away;
                    scoreHome += inning.Home;
                    if (scoreHome > scoreAway)
                    {
                        if (firstLead == null)
                        {
                            firstLead = game.HomeTeam.Name;
                            inningFirstLead = inningNum;
                        }
                        else
                        {
                            if (leadAway == true)
                            {
                                leadKept = false;
                            }
                        }
                        tied = false;
                        leadAway = false;
                    }
                    else if (scoreAway > scoreHome)
                    {
                        if (firstLead == null)
                        {
                            firstLead = game.AwayTeam.Name;
                            inningFirstLead = inningNum;
                        }
                        else
                        {
                            if (leadAway == false)
                            {
                                leadKept = false;
                            }
                        }
                        tied = false;
                        leadAway = true;
                    }
                    else
                    {
                        leadAway = false;
                        tied = true;
                        if (firstLead != null)
                        {
                            leadKept = false;
                        }
                    }
                }
                if (leadKept && inningFirstLead<9)
                {
                    gamestr += firstLead + " got the lead first in the " + inningFirstLead
                        + GetInningPrefix(inningFirstLead) + " and kept it. ";
                }
            }
            if (mentionYesterday == false)
            {
                gamestr = gamestr[0].ToString().ToUpper() + gamestr.Substring(1);
            }
            if (game.AwayScore > game.HomeScore)
            {
                gamestr += "The game finished " + game.AwayScore + " to " + game.HomeScore + ". ";
            }
            else
            {
                gamestr += "The final score was " + game.HomeScore + " to " + game.AwayScore + ". ";
            }
            if (game.AwayScore != game.HomeScore)
            {
                gamestr += "The winning pitcher was " + game.Status.WinningPitcher.FullName + ". ";
            }
            List<Player> stars = ThreeStarsCalculator.GetOffensiveThreeStarPlayers(game);
            gamestr += stars[0].NameDisplay + " had a great game (" + stars[0].GameLines[0] + ")";
            Output.FormatLongText(gamestr);
            Output.WriteLine();
        }

        /// <summary>
        /// Check rivalties
        /// </summary>
        private static bool CheckRivalry(Game game, string teamA, string teamB)
        {
            bool result = false;
            if (game.AwayTeam.Name.Equals(teamA) || game.HomeTeam.Name.Equals(teamA))
            {
                if (game.AwayTeam.Name.Equals(teamB) || game.HomeTeam.Name.Equals(teamB))
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// Format a next game
        /// </summary>
        public static void FormatNextGame(Game game)
        {
            string gamestr = " Don't miss the ";
            gamestr += game.AwayTeam.Name + " vs the " + game.HomeTeam.Name;
            gamestr += " at " + game.StartTime + " PM. ";
            //Output.WriteLine(gamestr);
            gamestr = "The " + game.AwayTeam.Name + " are " + game.AwayTeam.Wins;
            gamestr += "-" + game.AwayTeam.Losses + " and the";
            gamestr += " " + game.HomeTeam.Name + " are " + game.HomeTeam.Wins;
            gamestr += "-" + game.HomeTeam.Losses + ". ";
            if (CheckRivalry(game, "Red Sox", "Yankees"))
            {
                gamestr += "This is the ultimate rivalry. ";
            }
            else if (CheckRivalry(game, "Dodgers", "Giants"))
            {
                gamestr += "70 years of bad blood between those two. ";
            }
            else if (CheckRivalry(game, "Phillies", "Mets"))
            {
                gamestr += "A recent but tense National East rivalry. ";
            }
            else if (CheckRivalry(game, "Phillies", "Yankees"))
            {
                gamestr += "Two perennial world series contenders. ";
            }
            else if (CheckRivalry(game, "Phillies", "Braves"))
            {
                gamestr += "Familiar National East champions. ";
            }
            else if (CheckRivalry(game, "Cubs", "Cardinals"))
            {
                gamestr += "A classic rivalry. A classic match ahead! ";
            }
            else if (CheckRivalry(game, "Mets", "Yankees"))
            {
                gamestr += "The subway series! Who will rule the Big Apple? ";
            }
            else if (CheckRivalry(game, "White Sox", "Cubs"))
            {
                gamestr += "Northside vs Southside. A Chicago classic. ";
            }
            else if (CheckRivalry(game, "Rays", "Red Sox"))
            {
                gamestr += "No love lost between those two. ";
            }
            else if (game.HomeTeam.Division == game.AwayTeam.Division)
            {
                gamestr += "Divisional matchup. Always fun. ";
            }

            string homedecr = game.HomeProbable.Name
                    + " (" + game.HomeProbable.Wins
                        + "-" + game.HomeProbable.Losses + ", " + game.HomeProbable.ERA + ")"
                    + " for the " + game.HomeTeam.Name + ". ";
            if (game.HomeProbable.ERA == "-.--")
            {
                homedecr = game.HomeProbable.Name
                    + " will have his first start for the " + game.HomeTeam.Name + ". ";
            }

            if (game.AwayProbable != null && game.AwayProbable.Name.Trim() != string.Empty)
            { 
                gamestr += game.AwayProbable.Name
                    + " (" + game.AwayProbable.Wins
                        + "-" + game.AwayProbable.Losses + ", " + game.AwayProbable.ERA + ")"
                    + " is throwing for the "
                    + game.AwayTeam.Name + ". "
                    + homedecr;
            }
            Output.FormatLongText(gamestr);
            Output.WriteLine();
        }

        /// <summary>
        /// Get inning prefix
        /// </summary>
        private static string GetInningPrefix(int inning)
        {
            string inningSuffix = "th";
            if (inning == 1)
            {
                inningSuffix = "st";
            }
            else if (inning == 2)
            {
                inningSuffix = "nd";
            }
            else if (inning == 3)
            {
                inningSuffix = "rd";
            }
            return inningSuffix;
        }

        /// <summary>
        /// Get the pitcher/batter string
        /// </summary>
        private static string GetPitcherBatterString(Game game)
        {
            string line = "";
            string battingAbbr = game.HomeTeam.Abbrv;
            string pitchingAbbr = game.AwayTeam.Abbrv;
            if (game.Status.InningTop)
            {
                battingAbbr = game.AwayTeam.Abbrv;
                pitchingAbbr = game.HomeTeam.Abbrv;
            }
            Random rnd = new Random();
            switch (rnd.Next(10))
            {
                case 0:
                    line += "The pitcher is " + game.Status.CurrentPitcher.FullName;
                    line += ". He's facing " + game.Status.CurrentBatter.FullName + ". ";
                    break;
                case 1:
                    line += game.Status.CurrentPitcher.FullName + " is on the mound. ";
                    line += " At bat is " + game.Status.CurrentBatter.FullName + ". ";
                    break;
                case 2:
                    line += game.Status.CurrentBatter.FullName + " is facing ";
                    line += game.Status.CurrentPitcher.FullName + ". ";
                    break;
                case 3:
                    line += "The batter is " + game.Status.CurrentBatter.FullName;
                    line += ". He's facing " + game.Status.CurrentPitcher.FullName + ". ";
                    break;
                default:
                    line += game.Status.CurrentPitcher.FullName;
                    line += " is pitching to " + game.Status.CurrentBatter.FullName + ". ";
                    break;
            }
            return line;
        }

        /// <summary>
        /// Format a in progress game
        /// </summary>
        public static void FormatInProgressGame(Game game)
        {
            string gamstr = "";
            string battingAbbr = game.HomeTeam.Abbrv;
            string pitchingAbbr = game.AwayTeam.Abbrv; 
            if (game.Status.InningTop)
            {
                battingAbbr = game.AwayTeam.Abbrv;
                pitchingAbbr = game.HomeTeam.Abbrv; 
            }

            if (game.HomeScore == 0 && game.AwayScore == 0)
            {
                gamstr += "The " + game.AwayTeam.Name + " are battling the " + game.HomeTeam.Name;
                gamstr +=" at " + game.Stadium.Name + ". ";
                gamstr += "It's the " + game.Status.Inning + GetInningPrefix(game.Status.Inning) + " inning. ";
                gamstr += GetPitcherBatterString(game);
            }
            else if (game.HomeScore > game.AwayScore)
            {
                if (game.Status.InningTop)
                {
                    gamstr += "In the top of the " + game.Status.Inning + GetInningPrefix(game.Status.Inning) + ", ";
                }
                else
                {
                    gamstr += "In the bottom of the " + game.Status.Inning + GetInningPrefix(game.Status.Inning) + ", ";
                }
                gamstr +="the " + game.HomeTeam.Name + " are leading the " + game.AwayTeam.Name;
                gamstr +=" " + game.HomeScore + " to " + game.AwayScore + ". ";
                gamstr += game.Status.CurrentPitcher.FullName + " (" + pitchingAbbr + ")";
                gamstr += " is pitching to " + game.Status.CurrentBatter.FullName + " (" + battingAbbr + "). ";
            }
            else if (game.HomeScore < game.AwayScore)
            {
                gamstr += "In " + game.Stadium.Name + ", ";
                gamstr += "the " + game.AwayTeam.Name + " are leading the " + game.HomeTeam.Name;
                gamstr += " " + game.AwayScore + " to " + game.HomeScore + ". ";
                gamstr += "In the " + game.Status.Inning + GetInningPrefix(game.Status.Inning) + " inning, ";
                gamstr += game.Status.CurrentPitcher.FullName + " (" + pitchingAbbr + ")"; ;
                gamstr += " is pitching to " + game.Status.CurrentBatter.FullName + " (" + battingAbbr + "). ";
            }
            else
            {
                gamstr += "The " + game.AwayTeam.Name + " and the " + game.HomeTeam.Name;
                gamstr += " are tied " + game.AwayScore + " to " + game.HomeScore + ". ";
                gamstr += "In the " + game.Status.Inning + GetInningPrefix(game.Status.Inning) + " inning, ";
                gamstr += game.Status.CurrentPitcher.FullName + " (" + pitchingAbbr + ")"; ;
                gamstr += " is pitching to " + game.Status.CurrentBatter.FullName + " (" + battingAbbr + "). ";
            }


            if (game.AwayScore == game.HomeScore)
            {
                if (game.Status.Inning < 8)
                {
                    if (game.AwayScore == 0)
                    {
                        gamstr += "Still no scores. ";
                    }
                    //else
                    //{
                    //    gamstr += "The score is tied. ";
                    //}
                    
                }
                else if (game.Status.Inning < 10)
                {
                    gamstr += "We're near the end and the score is tied. ";
                }
                else
                {
                    gamstr += "In overtime and the score is tied. ";
                }
            }
            if (game.Status.RunnersOnBase[0] != null
                && game.Status.RunnersOnBase[1] != null
                && game.Status.RunnersOnBase[2] != null)
            {
                gamstr += "Bases are loaded! ";
            }
            else if (game.Status.RunnersOnBase[0] != null
                && game.Status.RunnersOnBase[1] != null)
            {
                gamstr += "There are men on first and second. ";
            }
            else if (game.Status.RunnersOnBase[0] != null
            && game.Status.RunnersOnBase[2] != null)
            {
                gamstr += "There are men on first and third. ";
            }
            else if (game.Status.RunnersOnBase[1] != null
            && game.Status.RunnersOnBase[2] != null)
            {
                gamstr += "There are men on second and third. ";
            }
            else if (game.Status.RunnersOnBase[0] != null)
            {
                //gamstr += "There's a man on first. ";
                gamstr += game.Status.RunnersOnBase[0].LastName + " is on first. ";
            }
            else if (game.Status.RunnersOnBase[1] != null)
            {
                //gamstr += "There's a man on second. ";
                gamstr += game.Status.RunnersOnBase[1].LastName + " is on second. ";
            }
            else if (game.Status.RunnersOnBase[2] != null)
            {
                gamstr += game.Status.RunnersOnBase[2].FullName + " is on third. ";
            }
            if (game.Status.Outs == 0)
            {
                gamstr += "No outs. ";
            }
            else if (game.Status.Outs == 1)
            {
                gamstr += "One out. ";
            }
            else if (game.Status.Outs == 2)
            {
                gamstr += "Two outs. ";
            }

            int hrtotal = 0;
            bool hrmention = false;
            foreach (Player player in game.Boxscore.AwayBatting)
            {
                hrtotal += player.Homeruns;
                if (player.Homeruns > 1)
                {
                    gamstr += player.NameDisplay + " has " + player.Homeruns + " in the game! ";
                    hrmention = true;
                }
            }
            Output.FormatLongText(gamstr);
            Output.WriteLine();
        }

        public void FormatMode(Scoreboard board)
        {
            if (board.GamesList.Games.Count == 0)
            {
                return;
            }
            //Output.WriteLine();
            Player[] stars = ThreeStarsCalculator.GetThreeStarPlayers(board.GamesList.Games);
            if (stars[0] != null && stars[0].GameScore > 0)
            {
                //if (ToolBox.YesterdayScores())
                //{
                //    Output.WriteLine(" Yesterday's three stars...");
                //}
                //else
                //{
                //    Output.WriteLine(" Today's three stars...");
                //}
                //Output.WriteLine("");
                FormatStar(1, stars[0]);
                FormatStar(2, stars[1]);
                FormatStar(3, stars[2]);
                //Output.WriteLine();
                if (ToolBox.YesterdayScores() == false && board.GamesList.SortedGames[0].Status.Indicator.IsInProgress)
                {
                    FormatInProgressGame(board.GamesList.SortedGames[0]);
                }
                else if (board.GamesList.SortedGames[0].Status.Indicator.IsTerminated)
                {
                    Output.WriteLine();
                    FormatTerminatedGame(board.GamesList.SortedGames[0], true);
                }
                if (board.GamesList.SortedGames.Count > 1)
                {
                    if (ToolBox.YesterdayScores() == false && board.GamesList.SortedGames[1].Status.Indicator.IsInProgress)
                    {
                        FormatInProgressGame(board.GamesList.SortedGames[1]);
                    }
                    else if (board.GamesList.SortedGames[1].Status.Indicator.IsTerminated)
                    {
                        FormatTerminatedGame(board.GamesList.SortedGames[1], false);
                    }
                    if (board.GamesList.SortedGames.Count > 2)
                    {
                        if (ToolBox.YesterdayScores() == false && board.GamesList.SortedGames[2].Status.Indicator.IsInProgress)
                        {
                            FormatInProgressGame(board.GamesList.SortedGames[2]);
                        }
                        else if (board.GamesList.SortedGames[2].Status.Indicator.IsTerminated)
                        {
                            FormatTerminatedGame(board.GamesList.SortedGames[2], false);
                        }
                    }
                }
            }
            else
            {
                string start = "Games will start at " + board.GamesList.Games[0].StartTime
                    + board.GamesList.Games[0].AmPm.ToLower() + ". "
                    + board.GamesList.Games.Count + " games today.";
                Output.FormatLongText(start);
                Output.WriteLine();
                FormatNextGame(board.GamesList.SortedGames[0]);
            }
            Output.WriteLine();
            if (board.Tweets != null && board.Tweets.Length > 0)
            {
                Output.FormatLongText(board.Tweets[0].Author.ToUpper().PadRight(20) + ": " + board.Tweets[0].Text);
                Output.FormatLongText(board.Tweets[1].Author.ToUpper().PadRight(20) + ": " + board.Tweets[1].Text);
                Output.FormatLongText(board.Tweets[2].Author.ToUpper().PadRight(20) + ": " + board.Tweets[2].Text);
                Output.FormatLongText(board.Tweets[3].Author.ToUpper().PadRight(20) + ": " + board.Tweets[3].Text);
                Output.FormatLongText(board.Tweets[4].Author.ToUpper().PadRight(20) + ": " + board.Tweets[4].Text);
            }
            Output.WriteLine();
        }

        private void FormatGames(Game gameA, Game gameB)
        {
            FormatGameLine(gameA, 1);
            FormatGameLine(gameB, 1);
            Output.WriteLine();
            FormatGameLine(gameA, 2);
            FormatGameLine(gameB, 2);
            Output.WriteLine();
            FormatGameLine(gameA, 3);
            FormatGameLine(gameB, 3);
            Output.WriteLine();
            FormatGameLine(gameA, 4);
            FormatGameLine(gameB, 4);
            Output.WriteLine();
            Output.WriteLine();
        }

        private void FormatGameLine(Game game, int line)
        {
            if (game == null)
            {
                return;
            }
            string output = "";
            switch(line)
            {
                case 1:
                    output = ToolBox.GetFormattedStringPadRight(" " + game.AwayTeam.Abbrv.ToUpper(), 6);
                    output += ToolBox.GetFormattedStringPadRight(game.AwayScore.ToString(), 3);
                    string indicator = "";
                    switch (game.Status.Indicator.Category)
                    {
                        case Indicator.IndicatorCategoy.TERMINATED: // F
                            indicator = "F";
                            break;
                        case Indicator.IndicatorCategoy.INTERRUPTED: // R
                            indicator = "R";
                            break;
                        case Indicator.IndicatorCategoy.IN_PROGRESS: // 1,2,3...
                            string stat = "BOT";
                            if (game.Status.InningTop)
                            {
                                stat = "TOP";
                            }
                            indicator = stat + game.Status.Inning;
                            break;
                        case Indicator.IndicatorCategoy.NEXT: // hour
                            indicator = game.StartTime;
                            break;
                        case Indicator.IndicatorCategoy.WARMUP: // hour
                            indicator = game.StartTime;
                            break;
                    }
                    output += ToolBox.GetFormattedStringPadRight(indicator, 10);
                    if (game.Status.Indicator.Category == Indicator.IndicatorCategoy.IN_PROGRESS)
                    {
                        if (game.Status.RunnersOnBase[0] != null
                            && game.Status.RunnersOnBase[1] != null
                            && game.Status.RunnersOnBase[2] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Bases are loaded!", WIDTH-19);
                        }
                        else if (game.Status.RunnersOnBase[0] != null
                            && game.Status.RunnersOnBase[1] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Men on 1st and 2nd", WIDTH - 19);
                        }
                        else if (game.Status.RunnersOnBase[0] != null
                        && game.Status.RunnersOnBase[2] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Men on 1st and 3rd", WIDTH - 19);
                        }
                        else if (game.Status.RunnersOnBase[1] != null
                        && game.Status.RunnersOnBase[2] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Men on 2nd and 3rd", WIDTH - 19);
                        }
                        else if (game.Status.RunnersOnBase[0] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Man on 1st", WIDTH - 19);
                        }
                        else if (game.Status.RunnersOnBase[1] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Man on 2nd", WIDTH - 19);
                        }
                        else if (game.Status.RunnersOnBase[2] != null)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" Man on 3rd", WIDTH - 19);
                        }
                        else
                        {
                            output += ToolBox.GetFormattedStringPadRight("", WIDTH - 19);
                        }
                    }
                    else if (game.Status.Indicator.Category == Indicator.IndicatorCategoy.TERMINATED)
                    {
                        output += ToolBox.GetFormattedStringPadRight("W: " + game.Status.WinningPitcher.LastName, WIDTH - 19);
                    }
                    else if (game.Status.Indicator.Category != Indicator.IndicatorCategoy.INTERRUPTED && game.AwayProbable != null)
                    {
                        output += ToolBox.GetFormattedStringPadRight(game.AwayProbable.Name, WIDTH - 19);
                    }
                    else
                    {
                        output += ToolBox.GetFormattedStringPadRight("", WIDTH - 19);
                    }
                    Output.Write(output);
                    break;
                case 2:
                    output = ToolBox.GetFormattedStringPadRight(" " + game.HomeTeam.Abbrv.ToUpper(), 6);
                    output += ToolBox.GetFormattedStringPadRight(game.HomeScore.ToString(), 13);
                    if (game.Status.Indicator.Category == Indicator.IndicatorCategoy.IN_PROGRESS)
                    {
                        if (game.Status.Outs > 1)
                        {
                            output += ToolBox.GetFormattedStringPadRight(" " + game.Status.Outs + " outs", WIDTH - 19);
                        }
                        else
                        {
                            output += ToolBox.GetFormattedStringPadRight(" " + game.Status.Outs + " out", WIDTH - 19);
                        }
                    }
                    else if (game.Status.Indicator.Category == Indicator.IndicatorCategoy.TERMINATED)
                    {
                        output += ToolBox.GetFormattedStringPadRight("L: " + game.Status.LosingPitcher.LastName, WIDTH - 19);
                    }
                    else if (game.Status.Indicator.Category != Indicator.IndicatorCategoy.INTERRUPTED && game.HomeProbable != null)
                    {
                        output += ToolBox.GetFormattedStringPadRight(game.HomeProbable.Name, WIDTH - 19);
                    }
                    else
                    {
                        output += ToolBox.GetFormattedStringPadRight("", WIDTH - 19);
                    }
                    Output.Write(output);
                    break;
                case 3:
                    output = ToolBox.GetFormattedStringPadRight(" ", WIDTH);
                    Output.Write(output);
                    break;
                case 4:
                    if (game.Status.Indicator.Category != Indicator.IndicatorCategoy.INTERRUPTED)
                    {
                        Player[] stars = ThreeStarsCalculator.GetThreeStarPlayers(game, false);
                        if (stars[0] != null && stars[0].GameScore > 0)
                        {
                            output = ToolBox.GetFormattedStringPadRight(" " + stars[0].LastName, WIDTH);
                        }
                    }
                    else
                    {
                        output = ToolBox.GetFormattedStringPadRight(" ", WIDTH);
                    }
                    Output.Write(output);
                    break;
            }
        }

        private void FormatScore(int score)
        {
            if (score < 0)
            {

            }
        }

        private void FormatIndicator(Game game)
        {
            if (game == null)
            {
                return;
            }
            
            switch (game.Status.Indicator.Category)
            {
                case Indicator.IndicatorCategoy.TERMINATED: // F
                    break;
                case Indicator.IndicatorCategoy.INTERRUPTED: // R
                    break;
                case Indicator.IndicatorCategoy.IN_PROGRESS: // 1,2,3...
                    break;
                case Indicator.IndicatorCategoy.NEXT: // hour
                    break;
                case Indicator.IndicatorCategoy.WARMUP: // hour
                    break;
            }
        }

        private void FormatGameLine2(Game game)
        {
            if (game == null)
            {
                return;
            }
            Output.Write(ToolBox.GetFormattedStringPadRight(" " + game.HomeTeam.Abbrv.ToUpper(), WIDTH));
        }

        private void FormatHeader()
        {
            Output.Write(ToolBox.GetFormattedStringPadRight(" TOP 7", 8));
            Output.Write(ToolBox.GetFormattedStringPadRight(" 1 2 3 4 5 6 7 8 9   R H E", WIDTH - 8));
        }

        private void FormatTeam()
        {
            Output.Write(ToolBox.GetFormattedStringPadRight(" BOS     1 2 3 4 5 6 7 8 9   R H E", WIDTH));
        }

        private void FormatBat()
        {
            Output.Write(ToolBox.GetFormattedStringPadRight(" BAT: James McNeill", WIDTH));
        }

        #endregion
    }
}
